#!/usr/bin/env python
# -*- coding: utf-8 -*-


import os
import sys
import traceback
import pytest
from utils.read_files_tools.case_automatic_control import TestCaseAutomaticGeneration
from utils.other_tools.models import NotificationType
from utils.other_tools.allure_data.allure_report_data import AllureFileClean
from utils.logging_tool.log_control import INFO
from utils.notify.wechat_send import WeChatSend
from utils.notify.ding_talk import DingTalkSendMsg
from utils.notify.send_mail import SendEmail
from utils.notify.lark import FeiShuTalkChatBot
from utils.other_tools.allure_data.error_case_excel import ErrorCaseExcel, ErrorCasesExcel

from utils import config


def run():

    # 从配置文件中获取项目名称
    try:
        INFO.logger.info(
            """
             _         _              _          _ _____         _   
            / \   _ __(_) ___  ___   / \   _ __ (_)_   _|__  ___| |_ 
           / _ \ | '__| |/ _ \/ __| / _ \ | '_ \| | | |/ _ \/ __| __|
          / ___ \| |  | |  __/\__ \/ ___ \| |_) | | | |  __/\__ \ |_ 
         /_/   \_\_|  |_|\___||___/_/   \_\ .__/|_| |_|\___||___/\__|
                                          |_|                                  
                  开始执行{}项目...
                """.format(config.project_name)
        )

        # 判断现有的测试用例，如果未生成测试代码，则自动生成
        TestCaseAutomaticGeneration().get_case_automatic()

        pytest.main(['-s', '-W', 'ignore:Module already imported:pytest.PytestWarning',
                     '--alluredir', './report/tmp', "--clean-alluredir"])

        """
                   --reruns: 失败重跑次数
                   --count: 重复执行次数
                   -v: 显示错误位置以及错误的详细信息
                   -s: 等价于 pytest --capture=no 可以捕获print函数的输出
                   -q: 简化输出信息
                   -m: 运行指定标签的测试用例
                   -x: 一旦错误，则停止运行
                   --maxfail: 设置最大失败次数，当超出这个阈值时，则不会在执行测试用例
                    "--reruns=3", "--reruns-delay=2"
        """

        os.system(r"allure  generate ./report/tmp -o ./report/html --clean")

        allure_data = AllureFileClean().get_case_count()
        notification_mapping = {
            NotificationType.FEI_SHU.value: FeiShuTalkChatBot(allure_data).post,
            NotificationType.DING_TALK.value: DingTalkSendMsg(allure_data).send_ding_notification,
            NotificationType.WECHAT.value: WeChatSend(allure_data).send_wechat_notification,
            NotificationType.EMAIL.value: SendEmail(allure_data).send_main
        }

        if config.notification_type != NotificationType.DEFAULT.value:
            notify_type = config.notification_type.split(",")

            notification_names = []  # 存储剥离后的通知类型名称
            for i in notify_type:
                stripped_i = i.lstrip(" ")
                notification_mapping[stripped_i]()
                # 0:不发送通知 1:飞书 2:钉钉通知 3:企业微信通知 4:邮箱通知
                if stripped_i == '1':
                    notification_names.append("飞书")
                elif stripped_i == '2':
                    notification_names.append("钉钉")
                elif stripped_i == '3':
                    notification_names.append("微信")
                elif stripped_i == '4':
                    notification_names.append("Email")
                else:
                    notification_names.append("默认通知类型")
            print("==============================================")
            print("已发送通知到: "+"、".join(notification_names))
            print("==============================================")

        # 收集运行失败的用例，整理成excel报告
        if config.excel_report:
            ErrorCasesExcel().creat_excel()

        # 程序运行之后，自动启动报告，如果不想启动报告，可注释这段代码
        # os.system(f"/opt/homebrew/bin/allure serve ./report/tmp -h 127.0.0.1 -p 9999")

    except Exception:
        # 如有异常，相关异常发送邮件
        e = traceback.format_exc()
        send_email = SendEmail(AllureFileClean.get_case_count())
        send_email.error_mail(e)
        raise


if __name__ == '__main__':
    run()

